import { defineStore } from "pinia";

export const useUserStore = defineStore("user", {
  state: () => ({
    userInfo: null,
    token: localStorage.getItem("token") || "",
    roles: [],
  }),

  getters: {
    isLoggedIn: (state) => !!state.token,
    username: (state) => state.userInfo?.username,
  },

  actions: {
    setToken(token) {
      this.token = token;
      localStorage.setItem("token", token);
    },

    setUserInfo(userInfo) {
      this.userInfo = userInfo;
    },

    setRoles(roles) {
      this.roles = roles;
    },

    logout() {
      this.token = "";
      this.userInfo = null;
      this.roles = [];
      localStorage.removeItem("token");
    },
  },
});
